package com.eatsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eatsystem.common.Result;
import com.eatsystem.dto.OrderDto;
import com.eatsystem.entity.OrderDetail;
import com.eatsystem.entity.Orders;
import com.eatsystem.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单提交
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders) {
        log.info("订单信息：{}", orders);
        orderService.submit(orders);

        return Result.success("下单成功");
    }

    /**
     * 管理端分页列表展示
     *
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize, Long number, String beginTime, String endTime) {
        //分页构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);

        //构造查询条件
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件,根据订单号来模糊查询
        queryWrapper.like(number != null, Orders::getNumber, number)
                //根据时间范围模糊查询
                .gt(StringUtils.isNotEmpty(beginTime), Orders::getOrderTime, beginTime)
                .lt(StringUtils.isNotEmpty(endTime), Orders::getOrderTime, endTime);

        //分页查询
        orderService.page(pageInfo, queryWrapper);

        return Result.success(pageInfo);
    }

    /**
     * 更新订单状态
     *
     * @param orders
     * @return
     */
    @PutMapping
    public Result<Orders> updateStatus(@RequestBody Orders orders) {
        log.info("订单状态：{}", orders);

        Integer status = orders.getStatus();
        if (status <= 5) {
            orders.setStatus(status);
        }
        orderService.updateById(orders);
        return Result.success(orders);
    }

    /**
     * 用户端分页展示
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page> userPage(int page, int pageSize) {
        //分页构造对象
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Page<OrderDto> pageDto = new Page<>(page, pageSize);

        //构造条件查询对象
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //将分页结果全部查询出来，没有分页条件
        //添加排序条件，根据更新时间排序
        queryWrapper.orderByDesc(Orders::getOrderTime);
        this.orderService.page(pageInfo, queryWrapper);

        //通过订单id（OrderId）查询对应的订单明细表（order_detail）
        LambdaQueryWrapper<OrderDto> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //对orderDto进行属性赋值
        List<Orders> records = pageInfo.getRecords();
        List<OrderDto> orderDtoList = records.stream().map((item) -> {
            OrderDto orderDto = new OrderDto();
            //给OrderDto对象赋值
            Long orderId = item.getId();//获取订单id
            List<OrderDetail> orderDetailList = this.orderService.getOrderDetailByOrderId(orderId);
            BeanUtils.copyProperties(item, orderDto);
            orderDto.setOrderDetailList(orderDetailList);
            return orderDto;
        }).collect(Collectors.toList());

        BeanUtils.copyProperties(pageInfo, pageDto, "records");
        pageDto.setRecords(orderDtoList);
        return Result.success(pageDto);
    }
}
